#include "TTAVEngineSDK.h"

#include "webrtc/base/scoped_ptr.h"
#include "webrtc/base/ipaddress.h"
#include "webrtc/base/ssladapter.h"
#include "webrtc/base/thread.h"
#ifdef WIN32
#include "webrtc/base/win32socketinit.h"
#include "webrtc/base/win32socketserver.h"
#else
#include "webrtc/base/thread.h"
#endif

#include "CallFactoryImpl.h"
#include "DeviceManagerImpl.h"


static bool g_is_initialized = false;
static rtc::scoped_ptr<CallFactoryImpl> call_factory_;
static rtc::scoped_ptr<DeviceManagerImpl> device_manager_;
static rtc::scoped_ptr<rtc::Thread> main_thread_;

bool TTAVEngineSDK::Initialize()
{
	if (!g_is_initialized)
	{
#ifdef WIN32
		rtc::EnsureWinsockInit();
#endif
		if (!main_thread_.get()) main_thread_.reset(
#ifdef WIN32
                                                          new rtc::Win32Thread()
#else
                                                          new rtc::Thread()
#endif
                                                         );
		rtc::ThreadManager::Instance()->SetCurrentThread(main_thread_.get());
		rtc::InitializeSSL();
		g_is_initialized = true;
	}
	return g_is_initialized;
}

void TTAVEngineSDK::Terminate()
{
	main_thread_.reset(NULL);
	rtc::ThreadManager::Instance()->SetCurrentThread(NULL);
	rtc::CleanupSSL();
	g_is_initialized = false;
}

CallFactory* TTAVEngineSDK::GetCallFactory()
{
	if (!call_factory_.get())
	{
		call_factory_.reset(new CallFactoryImpl());
	}
	assert(call_factory_.get());
	return call_factory_.get();
}

DeviceManager* TTAVEngineSDK::GetDeviceManager()
{
	if (!device_manager_.get())
	{
		device_manager_.reset(new DeviceManagerImpl());
	}
	assert(device_manager_.get());
	return device_manager_.get();
}

